//
//  ListViewController.swift
//  SwiftCode
//
//  Created by 于虎拴 on 2023/3/1.
//

import UIKit

extension ListViewController {
  struct Item {
    typealias Action = () -> Void
    let title: String
    let description: String
    let action: Action?
    let type: UIViewController.Type?
    
    init(title: String, description: String, action: Action?, type: AnyClass?) {
      self.title = title
      self.description = description
      self.type = type as? UIViewController.Type
      self.action = action
    }
    
    init(title: String, description: String) {
      self.init(title: title, description: description, action: nil, type: nil)
    }
    
    init(title: String, description: String, action: Action?) {
      self.init(title: title, description: description, action: action, type: nil)
    }
    
    init(title: String, description: String, type: AnyClass?) {
      self.init(title: title, description: description, action: nil, type: type)
    }
  }
}

class ListViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
  deinit {
    debugPrint(#fileID, self, #function, #line)
  }
  
  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    
    view.addSubview(tableView)
    tableView.snp.makeConstraints { $0.edges.equalToSuperview() }
  }
  
  lazy var tableView: UITableView = {
    let tableView = UITableView(frame: .zero, style: .plain)
    tableView.dataSource = self
    tableView.delegate = self
    return tableView
  }()
  
  open func getItemCount() -> Int {
    return 0
  }
  
  open func getItem(_ indexPath: IndexPath) -> Item {
    fatalError("getItem(:) has not been implemented")
  }
  
  open func onCreateCell(_ indexPath: IndexPath) -> UITableViewCell {
    let cell: UITableViewCell
    if let reusableCell = tableView.dequeueReusableCell(withIdentifier: UITableViewCell.reusableIdentifier) {
      cell = reusableCell
    } else {
      cell = UITableViewCell(style: .subtitle, reuseIdentifier: UITableViewCell.reusableIdentifier)
      cell.textLabel?.textColor = .label
      cell.detailTextLabel?.textColor = .secondaryLabel
    }
    return cell
  }
  
  open func onBindCell(_ cell: UITableViewCell, indexPath: IndexPath) {
    let item = getItem(indexPath)
    cell.textLabel?.text = item.title
    cell.detailTextLabel?.text = item.description
  }
  
  open func onItemSelect(_ indexPath: IndexPath) {
    let item = getItem(indexPath)
    if let action = item.action {
      action()
      return
    }
    if let type = item.type {
      let instance = type.init()
      instance.title = item.title
      navigationController?.pushViewController(instance, animated: true)
      return
    }
  }
  
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return getItemCount()
  }
  
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = onCreateCell(indexPath)
    onBindCell(cell, indexPath: indexPath)
    return cell
  }
  
  // MARK: - UITableViewDelegate
  
  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    // 选中并立即取消选中
    tableView.deselectRow(at: indexPath, animated: true)
    onItemSelect(indexPath)
  }
}
